﻿using MaterialManagement.API.Application.Command.ERP;
using MaterialManagement.Domain.ERP;
using MaterialManagement.ErrorCode;
using MaterialManagement.Infrastructure.Implement;
using MediatR;

namespace MaterialManagement.API.Application.Handler.ERP
{
    public class DelOutboundRecordHandler : IRequestHandler<DelOutboundRecordCommand, APIResult<int>>
    {
        private readonly DatabaseContext database;

        public DelOutboundRecordHandler(DatabaseContext database)
        {
            this.database = database;
        }
        public async Task<APIResult<int>> Handle(DelOutboundRecordCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = await database.SqlSugar.Queryable<OutboundRecordModel>().Where(x => x.OutboundId == request.Id).FirstAsync();
            if(res == null)
            {
                result.Message = "该领料单不存在";
                result.Code = APIEnum.Fail;
                return result;
            }
            res.IsDel = true;
            int r = await database.SqlSugar.Updateable(res).WhereColumns(x => x.OutboundId).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Message = "删除成功";
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "删除失败";
                result.Code = APIEnum.Fail;
            }
            return result;
        }
    }
}
